// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-12-21

#include "stdafx.h"
#include "msvr_server.h"
#include "msvr_config.h"
#include "msvr_user.h"
#include "msvr_room.h"
#include "msvr_userpool.h"
#include "dbmsg_mainhandler.h"
#include <common/svr_base/db_utility.h>
#include <common/base/utility.h>

namespace JoyServer
{

// class CJoyMainServer

CJoyMainServer::CJoyMainServer()
{
    m_tmUpdateRoom  = NULL;
    m_roomPort      = 0;
	m_tmShutdownRoom = NULL;
	m_tmShutdownRoom2 = NULL;
}

CJoyMainServer::~CJoyMainServer()
{
    JoyReleasePtrMap(m_chatMsg);
}

CJoyServerConfig* CJoyMainServer::AllocConfig()
{
    return new CJoyMainConfig();
}

CJoyUser* CJoyMainServer::AllocUser()
{
    return new CJoyMainUser();
}

CJoyUserSet* CJoyMainServer::AllocUserSet()
{
    return new CJoyMainRoom();
}

CJoyUserPool* CJoyMainServer::AllocUserPool()
{
    return new CJoyMainUserPool();
}

CJoyTmUserActive* CJoyMainServer::AllocTmUserActive()
{
    return __super::AllocTmUserActive();
}

CJoyDbMsgHandler* CJoyMainServer::AllocDbMsgHandler()
{
    return new CJoyDbMsgMainHandler();
}

CJoyMainRoom* CJoyMainServer::GetRoom(UINT roomId)
{
    CJoyAutoCS gate(m_userSetsLock);
    CJoyMainRoom* pRoom;
    if(!m_rooms.Lookup(roomId, pRoom))
    {
        return NULL;
    }
    return pRoom;
}

CJoyDbSource& CJoyMainServer::GetDb(const CString& dbName)
{
    return m_dbs[dbName];
}

bool CJoyMainServer::OpenGameDb(CJoyDbSource& source, int dbType)
{
    CJoyDbSession servSess;
    if(!JoyOpenDbSession(this, servSess, m_dbServer))
    {
        return false;
    }
    bool b = LoadDbSourceByType(this, source, servSess, dbType);
    return b;
}

} // namespace JoyServer